package Tetris;

import javax.swing.*;
import java.awt.*;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static Tetris.Tetris.ROWS;


public class Score {
    public int score = 0;
    public List<Integer> list;

    public int getScore() {
        return this.score;
    }

    public void addScore(int num) {
        this.score += num;
    }

    public void writeScore() throws IOException {
        FileOutputStream out = new FileOutputStream("score.txt");
        list.add(this.score);
        Collections.sort(list);
        for(int i : list) {
            out.write(i);
        }
        out.close();
    }

    public void readScore() throws IOException {
        FileInputStream in = new FileInputStream("score.txt");
        List<Integer> arrayList = new ArrayList<>();
        int tem;
        while ((tem = in.read()) != -1) {
            arrayList.add(tem);
        }
        in.close();
        Collections.sort(arrayList);
        this.list = arrayList;
    }

    public void getScore(int row, int col , int[][]block , int[][]data, JTextArea[][]text) {
        // row -= 1;
        for (int i = 0; i < block.length; i++) {
            boolean flag = true;
            for (int j = 0; j < data[0].length; j++) {
                if(row + i >= ROWS) break;
                if (data[row + i][j] == 0) flag = false;
            }
            if (flag) {
                for (int j = 0; j < data[0].length; j++) {
                    if(row + i >= ROWS) break;
                    data[row + i][j] = 0;
                    text[row + i][j].setBackground(Color.WHITE);
                }
                addScore(row);
            }
        }
    }
}
